package org.jeecg.chase.common.dataaccess.data;

import org.springframework.util.StringUtils;

/**
 * @author leikai
 * @version 1.0
 * @description: 分页请求对象封装类
 * @date 2021/07/01/0001 16:58
 */
public class PageRequest {

    //当前页数，默认为第一页
    protected int pageNo = 1;
    //页容量，默认为10条
    protected int pageSize = 10;
    //排序字句,如“col1 asc,col2 desc”
    protected String orderBy = "";
    //是否统计总数
    protected boolean countTotal = true;

    public PageRequest() {
    }

    public PageRequest(int pageNo, int pageSize, String orderBy) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.orderBy = orderBy;
    }

    /**
     * @return 当前第几页，从1开始
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * @param pageNo 设置当前第几页
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    /**
     * @return 页记录条数
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @param pageSize 设置页记录条数
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @return 排序子句，如“col1 asc,col2 desc”
     */
    public String getOrderBy() {
        return orderBy;
    }

    /**
     * @param orderBy 设置排序子句，如“col1 asc,col2 desc”
     */
    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    /**
     * @return the countTotal
     * 是否计算总记录数，默认为true
     */
    public boolean isCountTotal() {
        return countTotal;
    }

    /**
     * @param countTotal the countTotal to set
     *                   设置是否计算总记录数
     */
    public void setCountTotal(boolean countTotal) {
        this.countTotal = countTotal;
    }

    /**
     * 是否已设置排序字段,无默认值.
     */
    public boolean isOrderBySetted() {
        return (StringUtils.hasText(orderBy));
    }

    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置, 序号从0开始.
     */
    public int getOffset() {
        return ((pageNo - 1) * pageSize);
    }
}
